package com.hk.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.core.roleResult.PageRoleResult;
import com.hk.domain.User;
import com.hk.dto.TransferDto;
import com.hk.mapper.TransferMapper;
import com.hk.service.BusinessService;
import com.hk.service.ClueService;
import com.hk.service.TransferService;
import com.hk.service.UserService;
import com.hk.vo.BusinessAndClueVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    private TransferMapper transferMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private ClueService clueService;
    private final Long NUM=0L;
    @Override
    public PageRoleResult listTransfer(TransferDto transferDto ) {

//        先去条件分页用户表查询用户信息
        Page<User> pageuserList = userService.pageList(transferDto);
        List<User> userList = pageuserList.getRecords();

//      去商机表中查数量
       Map<String,Long> business= businessService.selectCount();
//       去线索表查数量
        Map<String,Long> clue= clueService.selectCount();
//      遍历集合，边遍历边查询,存到返回的类中
        ArrayList<BusinessAndClueVo> businessAndClueVoList = new ArrayList<>();

        log.info("何煜在遍历中查询");
        for (User user : userList) {
            BusinessAndClueVo businessAndClueVo = new BusinessAndClueVo();
            businessAndClueVo.setUserId(user.getUserId());
            businessAndClueVo.setUserName(user.getUserName());
            businessAndClueVo.setPhonenumber(user.getPhonenumber());
            businessAndClueVo.setCreateTime(user.getCreateTime());
            if (clue.get(user.getUserName())!=null){
                businessAndClueVo.setClueNum( clue.get(user.getUserName()));
            }else {
                businessAndClueVo.setClueNum(NUM);
            }

            if ((business.get(user.getUserName()))!=null){
                businessAndClueVo.setBusinessNum( (business.get(user.getUserName())));
            }else {
                businessAndClueVo.setBusinessNum(NUM);
            }

            businessAndClueVoList.add(businessAndClueVo);
        }
        PageRoleResult pageRoleResult = new PageRoleResult();
        pageRoleResult.setRows(businessAndClueVoList);
        pageRoleResult.setTotal((int)pageuserList.getTotal());
        pageRoleResult.setMsg("查询成功");
        pageRoleResult.setCode(200);
        return pageRoleResult;
    }

}
